package Teacher.Assessment;

import java.awt.event.*;
import java.sql.*;

import javax.swing.*;

import Database.*;
import Exception.*;
import General.*;
import Manager.Student.StudentSelectionFrame;


public class PanelAssessment extends ManagementPanel
{
	private JButton buttonReport;
	
	public PanelAssessment(CallDB callDB, Connection connection) throws CheckDataException, FormatScrollPaneException
	{
		super(callDB, connection);
		
		getButtonCreate().setEnabled(false);
		getButtonCreate().setVisible(false);
		getButtonModify().setEnabled(false);
		getButtonModify().setVisible(false);
		getButtonDelete().setEnabled(false);
		getButtonDelete().setVisible(false);
		
		createComponent();
		showComponent();
		
		addListeners();
	}

	public void createComponent()
	{
		buttonReport = new JButton("Generate Report !");
		buttonReport.setBounds(320, 381, 150, 50);
	}

	public void showComponent()
	{
		add(buttonReport);
		GraphicsSetting.setBackground(this, "image/Bg-Main.jpg", 0, 0, 800, 600);
	}

	public void addListeners()
	{
		MyMouseListener mouseListener = new MyMouseListener();
		tableLeft.addMouseListener(mouseListener);
		
		MyActionListener actionListener = new MyActionListener();
		buttonReport.addActionListener(actionListener);
	}

	private class MyMouseListener implements MouseListener
	{
		public void mouseClicked(MouseEvent arg0)
		{
			try
			{
				if(arg0.getSource() == tableLeft)
				{
					
					int selectedRow = tableLeft.convertRowIndexToModel(tableLeft.getSelectedRow()); //Get the index of the selected row
					GeneralInformation.createAssessmentDescriptModel(connection, tableLeft, selectedRow);
					GeneralMethod.setDescriptModel(GeneralInformation.getAssessmentModel());
					
					if(GeneralInformation.getAssessmentModel().getRowCount() > 0)
						refreshSuperDescriptTable();
				}
			}
			catch(CheckDataException | FormatScrollPaneException | LoadDriverException error)
			{
				TreatError.showError(error);
			}
		}
		
		
		public void mouseEntered(MouseEvent arg0){}
		public void mouseExited(MouseEvent arg0){}
		public void mousePressed(MouseEvent arg0){}
		public void mouseReleased(MouseEvent arg0){}
	}
	
	private class MyActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			try
			{
				if(arg0.getSource() == buttonReport)
				{
					if(tableDescript.getSelectedRow() != -1)
						new AssessmentReportFrame(callDB, connection, (Integer)tableLeft.getValueAt(tableLeft.getSelectedRow(), 2), (Integer)tableDescript.getValueAt(tableDescript.getSelectedRow(), 0));
					else
						throw new Layer8TableException();
				}
			}
			catch(CheckDataException | LoadDriverException | Layer8TableException error)
			{
				TreatError.showError(error);
			}
		}
	}
}
